const { query } = require("./mysql");
const {
	YD_datetimeToString,
	YD_whereSql,
	YD_setSql,
} = require("lib/utils/common");

/** @name 查询车辆-number **/
exports.yd_find = (options) => {
	const sql = `select
						*,
						${YD_datetimeToString("createTime")},
						${YD_datetimeToString("updateTime")}
						from ch_car
						${YD_whereSql([{ field: "number", type: "全等", value: options.number }])}
						`;
	return query(sql);
};

/** @name 添加车辆 **/
exports.yd_add = (options) => {
	let sql = `insert into ch_car set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 编辑车辆 **/
exports.yd_update = (options, condition) => {
	let sql = `update ch_car set ${YD_setSql(options)} where number='${
		condition.number
	}'`;
	return query(sql);
};

/** @name 编辑车辆-id **/
exports.yd_updateId = (options, condition) => {
	let sql = `update ch_car set ${YD_setSql(options)} where id=${condition.id}`;
	return query(sql);
};

/** @name 车辆列表 **/
exports.yd_list = (options) => {
	const sql = `select
						yc.id, yc.number, yc.count, yc.status, yc.color, yc.images, yc.note, yc.userId,
						yu.realname, yu.phone,
						${YD_datetimeToString("yc.createTime")},
						${YD_datetimeToString("yc.updateTime")}
						from ch_car yc
						left join ch_user yu on yc.userId=yu.uuid
						${YD_whereSql([
							{ field: "number", type: "模糊", value: options.number },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
							{ field: "status", type: "全等", value: options.status },
						])}
						order by yc.id desc
						limit ${(options.pageNum - 1) * options.pageSize},${options.pageSize}
            `;
	return query(sql);
};

/** @name 车辆总量 **/
exports.yd_count = (options) => {
	const sql = `select count(*) as count
						from ch_car yc
						left join ch_user yu on yc.userId=yu.uuid
						${YD_whereSql([
							{ field: "number", type: "模糊", value: options.number },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
							{ field: "status", type: "全等", value: options.status },
						])}
            `;
	return query(sql);
};

/** @name 导出车辆列表 **/
exports.yd_export = () => {
	const sql = `select
						yc.number, yc.count, yc.color, yc.note,
						yu.realname, yu.phone
						from ch_car yc
						left join ch_user yu on yc.userId=yu.uuid
						order by yc.id desc
            `;
	return query(sql);
};

/** @name 获取用户绑定车辆 **/
exports.yd_userCarlist = (options) => {
	const sql = `select yc.id, yc.number, yc.userId, yc.count, yc.status, yc.color, yc.images, yc.note,
						${YD_datetimeToString("yc.createTime")},
						${YD_datetimeToString("yc.updateTime")},
						yv.packageId,
						${YD_datetimeToString("yv.startTime")},
						${YD_datetimeToString("yv.endTime")},
						yr.orderCode
						from ch_car yc
						left join ch_vip yv on yv.number=yc.number
						left join (select * from ch_record where userId='${
							options.userId
						}' and status=0) yr on yr.number=yc.number
						${YD_whereSql([{ field: "yc.userId", type: "全等", value: options.userId }])}
						order by yc.id desc
            `;
	return query(sql);
};
